﻿//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="DylanBox">
// Copyright (c) DylanBox Corporation. All rights reserved.
// </copyright>
// <summary></summary>
// <reference>
/*
This example of the BitConverter.ToInt32( byte[ ], int )
method generates the following output. It converts elements
of a byte array to int values.

initial byte array
------------------
0F-00-00-00-00-80-00-00-10-00-00-F0-FF-00-CA-9A-3B-00-36-65-
C4-F1-FF-FF-FF-7F

index   array elements            int
-----   --------------            ---
    1      00-00-00-00              0
    0      0F-00-00-00             15
   21      F1-FF-FF-FF            -15
    6      00-00-10-00        1048576
    9      00-00-F0-FF       -1048576
   13      00-CA-9A-3B     1000000000
   17      00-36-65-C4    -1000000000
   22      FF-FF-FF-7F     2147483647
    2      00-00-00-80    -2147483648
*/
/*
 * Code Logic:
 * byte[] byteArray = {
             15,   0,   0,   0,   0, 128,   0,   0,  16,   0, 
              0, 240, 255,   0, 202, 154,  59,   0,  54, 101, 
            196, 241, 255, 255, 255, 127 };
 * 1. BitConverter.ToString(byteArray, 0, 20); convert the first 20 bytes to a string
 * get "0F-00-00-00-00-80-00-00-10-00-00-F0-FF-00-CA-9A-3B-00-36-65"
 * 2. BitConverter.ToString(byteArray, 20); convert the left bytes
 * get "C4-F1-FF-FF-FF-7F"
 * 3. int value = BitConverter.ToInt32(byteArray, 1); collect 4 bytes from the second item, 00-00-00-00, conver it to integer, result is 0
 *    BitConverter.ToInt32(byteArray, 0); result is 15, because "System.BitConverter.IsLittleEndian" is true, 00-00-00-0F
 *    BitConverter.ToInt32(byteArray, 12); result is -15, because "LittleEndian", F1-FF-FF-FF, actually is "FF-FF-FF-F1", It's negative，complement is “00-00-00-0F”
*/
// </reference>
// <author>Dylan</author>
//-----------------------------------------------------------------------

using System;

namespace BitConverterToInt32Sample
{
    // Example of the BitConverter.ToInt32 method.  
    class Example
    {
        static void Main(string[] args)
        {
            string str = string.Empty;
            Demo(str);
        }

        const string formatter = "{0,5}{1,17}{2,15}";

        // Convert four byte array elements to an int and display it.
        public static void BAToInt32(byte[] bytes, int index)
        {

            int value = BitConverter.ToInt32(bytes, index);

            string txt = String.Format(formatter, index,
                BitConverter.ToString(bytes, index, 4), value) + "\n";

            Console.WriteLine(txt);
        }

        // Display a byte array, using multiple lines if necessary.
        public static void WriteMultiLineByteArray(string txt, byte[] bytes)
        {
            const int rowSize = 20;
            int iter;

            txt += "initial byte array" + "\n";
            txt += "------------------" + "\n";

            for (iter = 0; iter < bytes.Length - rowSize; iter += rowSize)
            {
                txt += String.Format(
                    BitConverter.ToString(bytes, iter, rowSize));
                txt += "-" + "\n";
            }

            txt += String.Format(BitConverter.ToString(bytes, iter)) + "\n";
            txt += "\n";
        }

        public static void Demo(string result)
        {
            byte[] byteArray = {
             15,   0,   0,   0,   0, 128,   0,   0,  16,   0, 
              0, 240, 255,   0, 202, 154,  59,   0,  54, 101, 
            196, 241, 255, 255, 255, 127 };

            Console.WriteLine(string.Format("Is LittleEndian? {0}", System.BitConverter.IsLittleEndian ? "True" : "False"));            

            result += String.Format(
                "This example of the BitConverter.ToInt32( byte[ ], " +
                "int ) \nmethod generates the following output. It " +
                "converts elements \nof a byte array to int values.\n") + "\n";

            WriteMultiLineByteArray(result, byteArray);

            result += String.Format(formatter, "index", "array elements",
                "int") + "\n";
            result += String.Format(formatter, "-----", "--------------",
                "---") + "\n";

            Console.WriteLine(result);

           // Convert byte array elements to int values.
            BAToInt32(byteArray, 1);
            BAToInt32(byteArray, 0);
            BAToInt32(byteArray, 21);
            BAToInt32(byteArray, 6);
            BAToInt32(byteArray, 9);
            BAToInt32(byteArray, 13);
            BAToInt32(byteArray, 17);
            BAToInt32(byteArray, 22);
            BAToInt32(byteArray, 2);        

            Console.Read();
        }
    }
}
